﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using Ylh.BLL;
using System.Data;

public partial class Module_StaffManger_AddStaff : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.OnClientClick = ActiveWindow.GetHidePostBackReference ();


        if (!IsPostBack)
        {
            LoadDepartment();
            if (Request.QueryString["StaffID"] != null)
            {
                LoadStaffData(Request.QueryString["StaffID"].ToString());
                StaffID.Text = Request.QueryString["StaffID"].ToString();
            }
        }

    }
    #region 转载数据
    private void LoadStaffData(string StaffID)
    {
        LoadStaff(StaffID);
        LoadStaffInfo(StaffID);
    }

    private void LoadStaff(string StaffID)
    {
        Ylh.BLL.StaffBLL bll = new StaffBLL(DbConnectionStr);
        DataTable dt = bll.GetStaff(Convert.ToInt32(StaffID));
        this.StaffName.Text = dt.Rows[0]["StaffName"].ToString();
        Department.SelectedValue = dt.Rows[0]["DepartmentID"].ToString();
        RoleName.Text = dt.Rows[0]["RoleName"] != null ? dt.Rows[0]["RoleName"].ToString() : "";
        this.RoleID.Text = dt.Rows[0]["RoleID"] != null ? dt.Rows[0]["RoleID"].ToString() : "";
    }

    private void LoadStaffInfo(string StaffID)
    {
        Ylh.BLL.StaffInfoBLL bll = new Ylh.BLL.StaffInfoBLL(DbConnectionStr);
        Ylh.Model.TbStaffInfo model = bll.GetModel(Convert.ToInt32(StaffID));

        this.sex.SelectedValue = model.Sex;
        this.Birdthday.SelectedDate = Convert.ToDateTime(model.Birdthday);
        this.Tel.Text = model.Tel;
        this.Address.Text = model.Address;
        this.IDCard.Text = model.IDCard;
        if (model.InTime != null)
        {
            this.InTime.SelectedDate = Convert.ToDateTime(model.InTime);
        }
        this.Remark.Text = model.Remark;
        this.TelPhone.Text = model.TelPhone;
        this.LinkMan.Text = model.LinkMan;
        this.LinkNum.Text = model.LinkNum;
        this.Hometown.Text = model.Hometown;
        this.Marital.SelectedValue = model.Marital;
        this.HomeAddress.Text = model.HomeAddress;
        this.QQ.Text = model.QQ;
        this.mail.Text = model.mail;
    }

    private void LoadDepartment()
    {
        DepartmentBLL de = new DepartmentBLL(DbConnectionStr);

        DataTable dt = de.GetDepartMentInfo(-1);

        ExtAspNet.ListItem li;

        foreach (DataRow dr in dt.Rows)
        {
            li = new ExtAspNet.ListItem(dr["DepartmentName"].ToString(), dr["DepartmentID"].ToString());
            Department.Items.Add(li);
        }

    }
    #endregion

    #region 获取数据
    public Ylh.Model.TbStaffInfo GetStaffInfoModel()
    {
        int StaffID = Convert.ToInt32(Request.QueryString["StaffID"]);
        string Sex = this.sex.SelectedValue;
        DateTime Birdthday = DateTime.Parse(this.Birdthday.Text);
        string Tel = this.Tel.Text;
        string Address = this.Address.Text;
        string IDCard = this.IDCard.Text;
        DateTime InTime = DateTime.Parse(this.InTime.Text);
        string Remark = this.Remark.Text;
        string TelPhone = this.TelPhone.Text;
        string LinkMan = this.LinkMan.Text;
        string LinkNum = this.LinkNum.Text;
        string Hometown = this.Hometown.Text;
        string Marital = this.Marital.SelectedValue;
        string HomeAddress = this.HomeAddress.Text;
        string QQ = this.QQ.Text;
        string mail = this.mail.Text;



        Ylh.Model.TbStaffInfo model = new Ylh.Model.TbStaffInfo();
        model.StaffID = StaffID;
        model.Sex = Sex;
        model.Birdthday = Birdthday;
        model.Tel = Tel;
        model.Address = Address;
        model.IDCard = IDCard;
        model.InTime = InTime;
        model.Remark = Remark;
        model.TelPhone = TelPhone;
        model.LinkMan = LinkMan;
        model.LinkNum = LinkNum;
        model.Hometown = Hometown;
        model.Marital = Marital;
        model.HomeAddress = HomeAddress;
        model.QQ = QQ;
        model.mail = mail;

        return model;
    }
    #endregion



    protected void IsDeparture_CheckedChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(StaffID.Text.Trim()))
        {
            StaffBLL bll = new StaffBLL(DbConnectionStr);
            bll.UpdateStaffDeparture(Convert.ToInt32(StaffID.Text.Trim()), this.IsDeparture.Checked);
            Alert.Show("设置成功");
        }
        else
        {
            IsDeparture.Checked = false;
            Alert.Show("请先保存当期添加的员工信息");
            return;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        StaffInfoBLL infobll = new StaffInfoBLL(DbConnectionStr);
        StaffBLL blls = new StaffBLL(DbConnectionStr);


        if (string.IsNullOrEmpty(StaffName.Text))
        {
            Alert.Show("员工名称不能为空");
            return;
        }

        if (string.IsNullOrEmpty(InTime.Text))
        {
            Alert.Show("入职时间不能为空");
            return;
        }


        if (string.IsNullOrEmpty(Birdthday.Text))
        {
            Alert.Show("出生年月日不能为空");
            return;
        }


        Ylh.Model.TbStaffInfo staffinfo = GetStaffInfoModel();
        
        if (!string.IsNullOrEmpty(StaffID.Text.Trim()))
        {
            #region 修改方法
            
            infobll.UpdateStaffInfo(staffinfo);
            blls.UpdateStaff(this.StaffName.Text, StaffID.Text, Department.SelectedValue);
            Alert.Show("修改成功");
            #endregion
        }
        else
        {
         StaffID .Text = blls.InsertSinle(staffinfo,this.StaffName.Text,Department.SelectedValue);
         Alert.Show("添加成功");
        }
    }
    protected void RoleName_TriggerClick(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(StaffID.Text.Trim()))
        {
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(RoleName.ClientID, RoleID.ClientID) + Window1.GetShowReference("../RoleManger/RoleTree2.aspx?GetCode=1&StaffID=" + StaffID.Text.Trim(), "选择角色"));
        }
        else
        {
            Alert.Show("请先保存当期添加的员工信息");
            return;
        }

    }
}